// map.ts
import TileLayer from 'ol/layer/Tile';
import XYZ from 'ol/source/XYZ';
import { TileGrid } from 'ol/tilegrid'; // 用于自定义 tileGrid
import { get as getProjection } from 'ol/proj'
import { getTopLeft, getWidth } from 'ol/extent'



const token = '124a41c42a73be2d5e8eeb99ecce9f93'
const projection = getProjection('EPSG:4326')
const projectionExtent = projection.getExtent() // [-180, -90, 180, 90]
const origin = getTopLeft(projectionExtent) // 左上角 [-180, 90]

// 天地图 c 矩阵集的分辨率（共19级，z=0~18）
const resolutions = new Array(19)
for (let z = 0; z < 19; z++) {
	resolutions[z] = (getWidth(projectionExtent) / 256) / Math.pow(2, z)
}

const tileGrid = new TileGrid({
	origin: origin,
	resolutions: resolutions,
	tileSize: [256, 256],
})

const tiandituLayer = new TileLayer({
	source: new XYZ({
		url: `https://t{0-7}.tianditu.gov.cn/DataServer?T=vec_c&x={x}&y={y}&l={z}&tk=${token}`,
		projection: projection,
		tileGrid: tileGrid,
		wrapX: true,

	}),
})

const annotationLayer = new TileLayer({
	source: new XYZ({
		url: `https://t{0-7}.tianditu.gov.cn/DataServer?T=cva_c&x={x}&y={y}&l={z}&tk=${token}`,
		projection: projection,
		tileGrid: tileGrid,
		wrapX: true,

	}),
})

const tiandituImgLayer = new TileLayer({
	source: new XYZ({
		url: `https://t{0-7}.tianditu.gov.cn/DataServer?T=img_c&x={x}&y={y}&l={z}&tk=${token}`,
		projection: projection,
		tileGrid: tileGrid,
		wrapX: true,

	}),
	visible: false,
})

const imgAnnotationLayer = new TileLayer({
	source: new XYZ({
		url: `https://t{0-7}.tianditu.gov.cn/DataServer?T=cia_c&x={x}&y={y}&l={z}&tk=${token}`,
		projection: projection,
		tileGrid: tileGrid,
		wrapX: true,

	}),
	visible: false,
})

export {
	tiandituLayer,
	annotationLayer,
	tiandituImgLayer,
	imgAnnotationLayer
};